<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * Copyright (C) 2007  Robert Moss
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

class TipResult extends JLClass{
  
  var $id;
  var $project_id;
  var $match_id;
  var $user_id;
  var $tip;
  var $tip_home;
  var $tip_away;
  var $jokertip;
  
  
  function TipResult( &$db ) {
		$this->mosDBTable( '#__joomleague_tip_results', 'id', $db );
	}
  
  function saveTips($cid=null, $post=null, $usetipadmin=0){
    global $my;
  
    if (!is_array( $cid ) || count( $cid ) < 1) {
			$this->_error = "No items selected.";
			return false;
		}
    
    if (!is_array( $post )) {
			$this->_error = "No data.";
			return false;
		}
    
    $joomleague = new JoomLeague($this->_db);
    $joomleague->load($post["p"]);

    $query = "";
    $mailmessage = '';
    $mailjoker = 0;
    foreach($cid as $match_id) {
      $this->_db->setQuery(sprintf("SELECT m.match_date as round_date
                          FROM #__joomleague_matches m
                         WHERE m.project_id = %s AND m.published AND m.match_id=%s
                         ORDER BY m.match_date, match_id", $post["p"], $match_id));
      $result = $this->_db->loadResult();
      $round_date = $joomleague->getTimestamp($result);
	  $thistime=$joomleague->getTimestamp('',1);
	  $t1='';
	  $t2='';
	  $itip='';
	  $jokertip='';
      if(($round_date > $thistime) || (($my->id == $joomleague->tip_admin) && ($usetipadmin==1))){
      
        if($joomleague->mode == 0) {
  		  if (isset($post["tip_home_$match_id"])&&isset($post["tip_away_$match_id"])) {
		 	  if( isset($post["joker"]) && trim($post["joker"])=="j$match_id") $jokertip=1; else $jokertip='NULL';
			  if($t1 > $t2) $itip = 1;
			  elseif($t1 < $t2) $itip = 2;
			  elseif($t1 == $t2) $itip = 0;
			  $t1=trim($post["tip_home_$match_id"]);
			  $t2=trim($post["tip_away_$match_id"]);
              $query .= sprintf("REPLACE %s SET 
                                         id = '%s',
                                         project_id = %s,
                                         match_id = %s,
                                         tip = %s,
                                         user_id = %s,
                                         tip_home = %s, 
                                         tip_away = %s,
                                         joker = %s;\n",
                                         $this->_tbl, 
                                         $post["id_$match_id"],
                                         $post["p"],
                                         $match_id,
                                         $itip,
                                         $post["user_id"],
                                         $t1, 
                                         $t2,
                                         $jokertip
                                         ); 
		  } else {
			if ($post["id_$match_id"]!="") {
				$query .= sprintf("DELETE FROM %s WHERE (id=%s);\n", $this->_tbl, $post["id_$match_id"]);
			}
		  }
	    } else {
		  if (isset($post["tip_$match_id"])) {
		    $itip=$post["tip_$match_id"];
            $query .= sprintf("REPLACE %s SET 
                                       id = '%s',
                                       project_id = %s,
                                       match_id = %s,
                                       tip = %s,
                                       user_id = %s,
                                       tip_home = NULL, 
                                       tip_away = NULL;\n",
                                       $this->_tbl, 
                                       $post["id_$match_id"],
                                       $post["p"],
                                       $match_id,
                                       $post["tip_$match_id"],
                                       $post["user_id"]
                                       );
		  } else {
			if ($post["id_$match_id"]!="") {
				$query .= sprintf("DELETE FROM %s WHERE (id=%s);\n", $this->_tbl, $post["id_$match_id"]);
			}
		  }
        }
      }
      $match = new Match($this->_db);
		  $match->load($match_id);
		  $team1 = new JL_Team($this->_db);
		  $team1->load($match->matchpart1);
		  $team2 = new JL_Team($this->_db);
		  $team2->load($match->matchpart2);
		  $mailmessage .= "\n".$team1->name." - ".$team2->name.": ";
          if($joomleague->mode == 0) $mailmessage .= $t1." : ".$t2." (";
          $mailmessage .= $itip;
          if($joomleague->mode == 0) $mailmessage .= ")";
		  if ($jokertip==1) {
            $mailmessage .= " *";
            $mailjoker = 1;
          }
		  $mailmessage .= "\n";
    }
    if ($query!="") {
	    $this->_db->setQuery($query);
        //print $this->_db->getQuery();
        $this->_db->query_batch(false);
	}
	if (($joomleague->tip_joker == 1) && ($mailjoker == 1)) $mailmessage .= "\n* = "._JL_TIPS_JOKER."\n";
	return $mailmessage;
  }
  
}

?>
